package com.ssyt.hiringtwo.action.company;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ssyt.hiringtwo.action.BaseAction;
import com.ssyt.hiringtwo.common.Constant;
import com.ssyt.hiringtwo.entity.CityEntity;
import com.ssyt.hiringtwo.entity.CompanyEntity;
import com.ssyt.hiringtwo.entity.PositionEntity;
import com.ssyt.hiringtwo.entity.ProvinceEntity;
import com.ssyt.hiringtwo.entity.TradeEntity;
import com.ssyt.hiringtwo.servies.admin.TradeService;
import com.ssyt.hiringtwo.servies.company.AreaService;
import com.ssyt.hiringtwo.servies.company.CompanyService;
import com.ssyt.hiringtwo.servies.company.PositionService;
import com.ssyt.hiringtwo.servies.pub.UserService;

public class CompanyAction extends BaseAction {
	/* 创建业务层对象 */
	private PositionService positionService = new PositionService();
	private List<PositionEntity> positionList;
	private PositionEntity position;
	private int delayDay;//延期的天数
	private List<TradeEntity> tradeList;
	private TradeService tradeservice = new TradeService(); 
	private CompanyEntity company;
	private CompanyService companyService = new CompanyService();
	private UserService pubservice = new UserService();
	//地区
	private AreaService areaService = new AreaService();
	private List<ProvinceEntity> provinceList;
	private String provinceCode;
	/**
	 * 初始化公司职位管理页面
	 */
	public String CompanyPositionList() {
		// 查询职位管理信息
		positionList = positionService.listPositionService(getTalentId());
		return "toCompanyUI";
	}

	/**
	 * 初始化发布职位页面
	 */
	public String CompanyPositionAdd() {
	 provinceList = areaService.listProvince();
		return "toCompanyPositionAdd";
	}

	/**
	 * 初始化编辑发布职位页面
	 */
	public String CompanyPositionUpdate() {
	   provinceList = areaService.listProvince();
	    position = positionService.getPosition(position.getId());
		return "toCompanyPositionUpdate";
	}
    /**
     * 根据省编码获取所有的城市
     * 
     * @return
     */
    public void listCities() {
        List<CityEntity> cityList = areaService.listCitiesByProvinceCode(provinceCode);
        String jsonString = JSON.toJSONString(cityList);
        responseStr(jsonString);
    }

	/**
	 * 添加职位
	 * 
	 * @return
	 */
	public String addPosition() {
		position.setUserId(getTalentId());
		positionService.addPosition(position);
		return CompanyPositionList();
	}

	/**
	 * 删除职位管理
	 * 
	 * @return
	 */
	public void deletePosition() {
		int rows = positionService.deletePosition(position.getId());
		if (rows == Constant.EFFECT_ONE_ROW) {
			responseStr(Constant.RESPONSE_SUCCESS);
		} else {
			responseStr(Constant.RESPONSE_ERROR);
		}
	}
	
	 /**
     * 延期职位
     */
    public void delay() {
        int rows = positionService.updateValidDays(position.getId(), delayDay);
        if (rows == Constant.EFFECT_ONE_ROW) {
            responseStr(Constant.RESPONSE_SUCCESS);
        } else {
            responseStr(Constant.RESPONSE_ERROR);
        }
    }

	/**
	 * 初始化公司界面
	 * 
	 * @return
	 */
	public String CompanyUIgs() {
		return "CompanyUIgs";
	}

	/**
	 *初始化公司信息绑定页面 
	 * @return
	 */
	public String CompanyInfoUI(){
		 company = pubservice.getCompanInfo(getTalentId());
		 tradeList = tradeservice.listTrade();
		 return "toCompanyInfoUI";
	}
	
    /**
     * 更新职位信息
     * 
     * @return
     */
    public String editPosition() {
        positionService.editPosition(position);
        return CompanyPositionList();
    }
    /**
     * 保存公司信息
     * 
     * @return
     */
    public String saveCompanyInfo() {
        company.setUserId(getTalentId());
        int row = 0;
        if (company.getId() == 0) {
            // 新增
            row = companyService.saveCompanyInfo(company);
        } else {
            // 更新
            row = companyService.updateCompanyInfo(company);
        }

        if (row == 1) {
            return CompanyPositionList();
        } else {
            return CompanyInfoUI();
        }
    }
    

	public List<PositionEntity> getPositionList() {
		return positionList;
	}

	public void setPositionList(List<PositionEntity> positionList) {
		this.positionList = positionList;
	}

	public PositionEntity getPosition() {
		return position;
	}

	public void setPosition(PositionEntity position) {
		this.position = position;
	}

	public int getDelayDay() {
		return delayDay;
	}

	public void setDelayDay(int delayDay) {
		this.delayDay = delayDay;
	}

	public List<TradeEntity> getTradeList() {
		return tradeList;
	}

	public void setTradeList(List<TradeEntity> tradeList) {
		this.tradeList = tradeList;
	}

	public PositionService getPositionService() {
		return positionService;
	}

	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}

	public TradeService getTradeservice() {
		return tradeservice;
	}

	public void setTradeservice(TradeService tradeservice) {
		this.tradeservice = tradeservice;
	}

	public CompanyEntity getCompany() {
		return company;
	}

	public void setCompany(CompanyEntity company) {
		this.company = company;
	}

	public List<ProvinceEntity> getProvinceList() {
		return provinceList;
	}

	public void setProvinceList(List<ProvinceEntity> provinceList) {
		this.provinceList = provinceList;
	}

	public String getProvinceCode() {
		return provinceCode;
	}

	public void setProvinceCode(String provinceCode) {
		this.provinceCode = provinceCode;
	}

}
